package com.dwin.dingnuo.http;

import android.util.Log;


import com.dwin.dingnuo.http.beans.BackModel;
import com.dwin.dingnuo.http.beans.ResponseTokenVerify;
import com.dwin.dingnuo.http.response.RspDeviceDetail;
import com.dwin.dingnuo.http.response.RspDeviceList;
import com.dwin.dingnuo.http.response.RspImageVCode;

import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;

/**
 * 鼎诺4G项目，
 */
public class MyRetrofit {
    public static final String platform = "1";

    private static final boolean isDebugFlag = true;
    private static final String BASE_URL_DEV = "http://api.dingnuo.dwinhmi.com.cn/";
    private static final String BASE_URL_ONLINE = "http://api.dingnuo.dwinhmi.com.cn/";

    private static String getBaseUrl() {
        return isDebugFlag ? BASE_URL_DEV : BASE_URL_ONLINE;
    }

    private static MyRetrofit instance = null;

    public static synchronized MyRetrofit getInstance() {
        // 这个方法比上面有所改进，不用每次都进行生成对象，只是第一次
        // 使用时生成实例，提高了效率！
        if (instance == null)
            instance = new MyRetrofit();
        return instance;
    }

    static Retrofit retrofit;
    static UserMgrService userMgrService;

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) { //打印retrofit日志
            Log.e("RetrofitLog", "retrofitBack = " + message);
        }
    });

    private MyRetrofit() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()//okhttp设置部分，此处还可再设置网络参数
                .addInterceptor(loggingInterceptor).build();
        retrofit = new Retrofit.Builder().baseUrl(getBaseUrl())
                .client(client)//此client是为了打印信息
                .addConverterFactory(GsonConverterFactory.create()).build();
        userMgrService = retrofit.create(UserMgrService.class);
    }

    // 获取图形码
    public static void getImageVCode(String language, String key, int type, Callback<RspImageVCode> callback) {
        Call<RspImageVCode> call = userMgrService.getImageVCode(platform, language, key, type);
        call.enqueue(callback);
    }

    // 登录
    public static void clientLogin(String language, RequestBody body, Callback<ResponseTokenVerify> callback) {
        Call<ResponseTokenVerify> call = userMgrService.clientLogin(platform, language, body);
        call.enqueue(callback);
    }


    // 修改密码
    public static void clientPwdUpdate(String language, String auth, RequestBody body, Callback<BackModel> callback) {
        Call<BackModel> call = userMgrService.clientPwdUpdate(platform, language, auth, body);
        call.enqueue(callback);
    }


    // 获取邮箱验证码
    public static void getVCode(String language, Map<String, String> queries, Callback<BackModel> callback) {
        Call<BackModel> call = userMgrService.getVCode(platform, language, queries);
        call.enqueue(callback);
    }

    // 忘记密码
    public static void passwordReset(String language, RequestBody body, Callback<ResponseTokenVerify> callback) {
        Call<ResponseTokenVerify> call = userMgrService.passwordReset(platform, language, body);
        call.enqueue(callback);
    }

    // ---------------------------------------------------------------------------------------------
    // 设备相关接口
    public static void getDeviceList(Map<String, String> headers, Map<String, String> queries, Callback<RspDeviceList> callback) {
        Call<RspDeviceList> call = userMgrService.getDeviceList(headers, queries);
        call.enqueue(callback); // 异步发送请求
    }

    // 获取设备详情
    public static void getDeviceDetail(String auth, String language, Map<String, String> queries, Callback<RspDeviceDetail> callback) {
        Call<RspDeviceDetail> call = userMgrService.getDeviceDetail(platform, language, auth, queries);
        call.enqueue(callback); // 异步发送请求
    }

    // 修改设备名称
    public static void deviceEdit(String auth, String language, RequestBody body, Callback<BackModel> callback) {
        Call<BackModel> call = userMgrService.deviceEdit(platform, language, auth, body);
        call.enqueue(callback);
    }


    // 删除设备
    public static void deviceDelete(String auth, String language, RequestBody body, Callback<BackModel> callback) {
        Call<BackModel> call = userMgrService.deviceDelete(platform, language, auth, body);
        call.enqueue(callback);
    }

    // 扫码添加设备
    public static void addDeviceByQRCode(String auth, String language, RequestBody body, Callback<BackModel> callback) {
        Call<BackModel> call = userMgrService.addDeviceByQRCode(platform, language, auth, body);
        call.enqueue(callback);
    }

    // ---------------------------------------------------------------------------------------------
    // 日志添加接口
    public static void addLogs(String auth, String language, RequestBody body, Callback<BackModel> callback) {
        Call<BackModel> call = userMgrService.addClientLogs(platform, language, auth, body);
        call.enqueue(callback);
    }


}
